/*
 * SocketManager.h
 *
 *  Created on: May 8, 2011
 *      Author: michael
 */

#ifndef SOCKETMANAGER_H_
#define SOCKETMANAGER_H_

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <google/protobuf/message_lite.h>
#include "ProcessMessage.h"

namespace DerbyServ
{

class TcpServer;
class MainManager;
class RawData;

class Hello;


//TODO make thread-safe
class SocketManager
{
public:
	SocketManager();
	virtual ~SocketManager();

	void Start();
	void Send(const google::protobuf::MessageLite & message, enum Messages::MessageTypes type, int connectionId);
	void ReadAll(MainManager & mgr);

	std::vector<int> GetConnectionIds() const;

private:
	void ProcessRawData(MainManager & mgr, RawData & raw, int connectionId);

private:
	boost::asio::io_service _ioService;
	boost::scoped_ptr<TcpServer> _tcpServer;
	std::map<enum Messages::MessageTypes, ProcessMessage> messageProcessors;
};
typedef boost::shared_ptr<SocketManager> SocketManagerPtr;


} //namespace

#endif /* SOCKETMANAGER_H_ */
